<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
    >
      <!-- 登录地址 -->
      <el-form-item :label="$t('onLine.label.address')" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          :placeholder="$t('onLine.input.address')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 用户名称 -->
      <el-form-item :label="$t('onLine.label.user')" prop="userName">
        <el-input
          v-model="queryParams.userName"
          :placeholder="$t('onLine.input.user')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          <!-- 搜索 -->
          {{ $t('onLine.button.search') }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          <!-- 重置 -->
          {{ $t('onLine.button.reset') }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
	  max-height='520'
      :data="list.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
      style="width: 100%;"
    >
      <!-- 序号 -->
      <el-table-column
        :label="$t('onLine.table.order')"
        type="index"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- 会话编号 -->
      <el-table-column
        :label="$t('onLine.table.number')"
        align="center"
        prop="tokenId"
        :show-overflow-tooltip="true"
      />
      <!-- 登录名称 -->
      <el-table-column
        :label="$t('onLine.table.author')"
        align="center"
        prop="userName"
        :show-overflow-tooltip="true"
      />
      <!-- 部门名称 -->
      <el-table-column
        :label="$t('onLine.table.dept')"
        align="center"
        prop="deptName"
      />
      <!-- 主机 -->
      <el-table-column
        :label="$t('onLine.table.main')"
        align="center"
        prop="ipaddr"
        :show-overflow-tooltip="true"
      />
      <!-- 登录地点 -->
      <el-table-column
        :label="$t('onLine.table.site')"
        align="center"
        prop="loginLocation"
        :show-overflow-tooltip="true"
      />
      <!-- 浏览器 -->
      <el-table-column
        :label="$t('onLine.table.browser')"
        align="center"
        prop="browser"
      />
      <!-- 操作系统 -->
      <el-table-column
        :label="$t('onLine.table.system')"
        align="center"
        prop="os"
      />
      <!-- 登录时间 -->
      <el-table-column
        :label="$t('onLine.table.loginTime')"
        align="center"
        prop="loginTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime) }}</span>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column
        :label="$t('onLine.table.handle')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- 强退 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleForceLogout(scope.row)"
            v-hasPermi="['monitor:online:forceLogout']"
          >
            {{ $t('onLine.table.exit') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
    />
  </div>
</template>

<script>
import { list, forceLogout } from '@/api/monitor/online'

export default {
  name: 'Online',
  data () {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 20,
      // 查询参数
      queryParams: {
        ipaddr: undefined,
        userName: undefined
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询登录日志列表 */
    getList () {
      this.loading = true
      list(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 强退按钮操作 */
    handleForceLogout (row) {
      this.$confirm(
        '是否确认强退名称为"' + row.userName + '"的数据项?',
        this.$t('ConLog.dialog.warning'), // 警告
        {
          confirmButtonText: this.$t('ConLog.dialog.button.confirm'), // 确定
          cancelButtonText: this.$t('ConLog.dialog.button.cancel'), // 取消
          type: 'warning'
        }
      )
        .then(function () {
          return forceLogout(row.tokenId)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('强退成功')
        })
        .catch(error => {
          return error
        })
    }
  }
}
</script>
